/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is 
 * James Lutsko.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package autoteam;

import java.io.*;
import java.util.*;

import jxl.*;

public class Child extends Virtue implements Serializable  {

    public String firstName = null;
    public String lastName = null;
    public Integer id = null;
    private Boolean nights[];
    //Boolean nights[];

    transient private Boolean nights_intersection[];
    //transient Boolean nights_intersection[];

    public String phone;
    public String email;
    public String comment = null;
    public Integer height;
    public Integer mass;
    public Date birthDate = new Date();
    static int Version = 1;
    static int Release = 0;
    public boolean coachKid = false;

    private Team team = null;


    public String toString(){return lastName+", "+firstName;}
    public String export(){return lastName+","+firstName+"\t"+email+"\t"+phone;}
    public boolean isCoachKid(){return coachKid;}

    public Child(int skillNumber)
    {
	nights = new Boolean[5];
	skill = new Double[skillNumber];
	weight = new Double[skillNumber];

	experience = 0;
	for(int i=0;i<skillNumber;i++)
	    skill[i] = new Double(0.0);
	for (int i = 0; i < skillNumber; i++)
		weight[i] = new Double(1.0);
	parseNights(null,true);
    }

    public void initialize()
    {
	team = null;
	totalSkill = 0;
	for(int i=0;i<skill.length;i++)
	    totalSkill += (skill[i] == null ? 0 : skill[i].doubleValue() * weight[i].doubleValue());

	if(nights_intersection == null) nights_intersection = new Boolean[5];
    }

    public void initializeNights()
    {
	if(nights_intersection == null) nights_intersection = new Boolean[5];
	for(int i=0;i<=4;i++) nights_intersection[i] = nights[i];
    }

    public boolean nightAgreesWith(Child c, int i){return (nights_intersection[i].booleanValue() == c.nights_intersection[i].booleanValue());}
    public void setNight(int i, Boolean b){nights_intersection[i] = b;}
    public boolean checkNight(Team t){return nights_intersection[t.night].booleanValue();}
    public boolean checkNight(int n){ return nights[n].booleanValue();}
    public Boolean getBaseNight(int i) {return nights[i];}

    public void setBaseNight(int i, Boolean b) { nights[i] = b;}
    public void setTransientNight(int i, Boolean b) { if(nights_intersection != null) nights_intersection[i] = b;}

    public void setTeam(Team _t){team = _t; team.addedKid();}
    public boolean isTeam(Team _t){return (team == _t);}
    public boolean hasTeam(){return (team != null);}
    public Team getTeam(){ return team;}


    Child(String input, String headers) {								// added
    	
    	// String[] header = headers.split(",");
    	String[] token = input.split(",");
    	
    	nights = new Boolean[5];
    	skill = new Double[token.length - 10];
    	weight = new Double[token.length - 10];
    	
    	lastName = token[0];
    	firstName = token[1];
    	id = Integer.parseInt(token[2]);
    	phone = token[3];
    	email = token[4];
    	height = Integer.parseInt(token[5]);
    	mass = Integer.parseInt(token[6]);
    	experience = Integer.parseInt(token[7]);
    	String _theGoodNights = token[8];
    	parseNights(_theGoodNights, true);
    	for (int i = 0; i < skill.length; i++)
    		skill[i] = Double.parseDouble(token[9 + i]);
    	for (int i = 0; i < weight.length; i++)
    		weight[i] = 1.0;
    	if (token.length == 13)
    		comment = token[12];
    }
    

    Child(String input, DataMap dataMap) 
    {

	nights = new Boolean[5];
	skill = new Double[dataMap.getSkillNumber()];
	weight = new Double[dataMap.getSkillNumber()];

	String[] fields = input.split(",",-1);
	lastName   = dataMap.getStringField(1,fields);
	firstName  = dataMap.getStringField(2,fields);
	phone      = dataMap.getStringField(3,fields);
	email      = dataMap.getStringField(4,fields);
	id         = dataMap.getIntField(5,fields); 
	comment    = dataMap.getStringField(6,fields);
	height     = dataMap.getIntField(7,fields); 
	experience = dataMap.getIntValueField(8,fields); 
	String _theBadNights = dataMap.getStringField(9,fields);
	String _theGoodNights = dataMap.getStringField(10,fields);
	for(int i=0;i<skill.length;i++)
	    {
		skill[i] = dataMap.getSkillField(i,fields); 
	    }
	for (int i = 0; i < weight.length; i++) {
		weight[i] = 1.0;
	}
	if(_theBadNights != null)
	    parseNights(_theBadNights,false);
	else parseNights(_theGoodNights,true);
    }

    Child(Cell[] cells, DataMap dataMap) 
    {

	nights = new Boolean[5];
	skill = new Double[dataMap.getSkillNumber()];
	weight = new Double[dataMap.getSkillNumber()];

	if(cells == null || cells.length == 0) return;

	String[] fields = new String[cells.length];
	for(int i=0;i<fields.length;i++)
	    {
		Cell c = cells[i];
		if(c == null) fields[i] = null;
		else {
		    Object val = c.getContents();

		    if (c.getType() == CellType.DATE)
			{
			    double age = 0.0;
			    Calendar cal = Calendar.getInstance();
			    cal.setTime(new Date());
			    age = cal.get(cal.YEAR);
			    age += (1.0/12.0)*cal.get(cal.MONTH);

			    DateCell dc = (DateCell) c;
			    Date d = dc.getDate();
			    if(d == null) val = null;
			    else {
				cal.setTime(d);
				age -= cal.get(cal.YEAR);
				age -= (1.0/12.0)*cal.get(cal.MONTH);
			    }
			    val = new Double(age);
			}
		    fields[i] = (val == null ? null : val.toString());
		}
	    }


	lastName   = dataMap.getStringField(1,fields);
	firstName  = dataMap.getStringField(2,fields);
	phone      = dataMap.getStringField(3,fields);
	email      = dataMap.getStringField(4,fields);
	id         = dataMap.getIntField(5,fields); 
	comment    = dataMap.getStringField(6,fields);
	height     = dataMap.getIntField(7,fields); 
	experience = dataMap.getIntValueField(8,fields); 
	String _theBadNights = dataMap.getStringField(9,fields);
	String _theGoodNights = dataMap.getStringField(10,fields);
	for(int i=0;i<skill.length;i++)
	    {
		skill[i] = dataMap.getSkillField(i,fields); 
	    }
	for (int i = 0; i < weight.length; i++) {
		weight[i] = 1.0;
	}
	if(_theBadNights != null)
	    parseNights(_theBadNights,false);
	else parseNights(_theGoodNights,true);
    }

    void parseNights(String theNights, boolean isGood)
    {
	
	if(theNights != null)
	    {
		nights[0] = (theNights.indexOf("Monday") < 0 
			     ? (isGood ? Boolean.FALSE : Boolean.TRUE) : (isGood ? Boolean.TRUE : Boolean.FALSE));
		nights[1] = (theNights.indexOf("Tuesday") < 0 
			     ? (isGood ? Boolean.FALSE : Boolean.TRUE) : (isGood ? Boolean.TRUE : Boolean.FALSE));
		nights[2] = (theNights.indexOf("Wednesday") < 0 
			     ? (isGood ? Boolean.FALSE : Boolean.TRUE) : (isGood ? Boolean.TRUE : Boolean.FALSE));
		nights[3] = (theNights.indexOf("Thursday") < 0 
			     ? (isGood ? Boolean.FALSE : Boolean.TRUE) : (isGood ? Boolean.TRUE : Boolean.FALSE));
		nights[4] = (theNights.indexOf("Friday") < 0 
			     ? (isGood ? Boolean.FALSE : Boolean.TRUE) : (isGood ? Boolean.TRUE : Boolean.FALSE));
	    } else for(int i=0;i<=4;i++) nights[i] = Boolean.TRUE;
    }
    public int hashCode(){return id.intValue();}

    static final long serialVersionUID = 1144566015356924188L;

}



